package fun.codedesign.yinxue.code.php2java.service.visitor.listener;

import fun.codedesign.yinxue.code.php2java.rule.Php2JavaRuleChain;
import fun.codedesign.yinxue.code.engine.antlr.php.PhpParser;
import fun.codedesign.yinxue.code.engine.antlr.php.PhpParserBaseListener;
import fun.codedesign.yinxue.code.php2java.model.Php2JavaContext;
import fun.codedesign.yinxue.code.php2java.model.PhpDesc;
import org.yinxue.swing.unit.model.ClassDesc;

public class Php2JavaPrintListener extends PhpParserBaseListener {
    Php2JavaRuleChain rule = new Php2JavaRuleChain();
    Php2JavaContext php2JavaContext = new Php2JavaContext();

    StringBuilder builder = new StringBuilder();

    PhpDesc phpDesc = new PhpDesc();
    ClassDesc classDesc = new ClassDesc();


    // namespace phpbb\cron;
    // \phpbb\cron\task\wrapper 类似这样的也会进入到该节点
    @Override
    public void enterQualifiedNamespaceName(PhpParser.QualifiedNamespaceNameContext ctx) {
        String phpNamespace = ctx.getText();
        if (phpNamespace.contains("namespace")) {
            String delnamespace = rule.replaceKeywordNamespace(phpNamespace);
            String packageName = rule.packageReplaceSlashWithDot(delnamespace);
            println("package " + packageName + ";", "");
        } else {
            // 非 namespace的过滤
        }
    }

    // use common\library\CurlTrait;
    @Override
    public void enterUseDeclaration(PhpParser.UseDeclarationContext ctx) {
        // 获取use 后的路径，转换为package
        String originPath = ctx.getChild(1).getText();
        String importLine = rule.useReplaceSlashWithDot(originPath);
        println("import " + importLine + ";", "");
    }

    // class manager { }
    @Override
    public void enterClassDeclaration(PhpParser.ClassDeclarationContext ctx) {
        //  manager -> Manager
        String originTypeName = ctx.identifier().getText();
        String newTypeName = rule.typeName2UpperCaseFirstChar(originTypeName);
        println("public class " + newTypeName + " {","");
        phpDesc.className = originTypeName;
        classDesc.simpleName = newTypeName;
    }

    @Override
    public void exitClassDeclaration(PhpParser.ClassDeclarationContext ctx) {
        print("\n}"," ----exitClassDeclaration");
    }

    // 方法 构造器 实例变量等
    @Override
    public void enterClassStatement(PhpParser.ClassStatementContext ctx) {
        if (ctx.identifier() == null) {
            // 过滤掉实例变量
            println(ctx.getText(),"  ----enterClassStatement");
        } else {
            // 1. 构建一个方法变量声明节点
            String methodName = ctx.identifier().getText();
            if (methodName.equals("__construct")) {
                print("public "+ classDesc.simpleName,"");
            } else {
                // {} 由下方 MethodBody来闭合 () 由 enterArguments来 闭合  TODO public void需要配合return
                print("public void " + methodName,"");
            }
        }
    }

    @Override
    public void enterFormalParameterList(PhpParser.FormalParameterListContext ctx) {
        print("("+ctx.getText()+")","  ----enterFormalParameterList");
    }

    @Override
    public void enterMethodBody(PhpParser.MethodBodyContext ctx) {
        print(" {\n","");
    }

    @Override
    public void exitMethodBody(PhpParser.MethodBodyContext ctx) {
        print("}\n","");
    }

    @Override
    public void enterYieldExpression(PhpParser.YieldExpressionContext ctx) {
        println(ctx.getText(),  "  ----enterYieldExpression");
    }

    @Override
    public void enterVariableInitializer(PhpParser.VariableInitializerContext ctx) {
        // 1. 构建一个变量节点
        String varName = ctx.VarName().getText();
        String newVarName = rule.varReplaceDollar(varName);

        String initStatement = null;
        if (ctx.constantInitializer() != null) {
            initStatement = ctx.constantInitializer().getText();
        }
        //print(ctx.getText(),"  ----enterVariableInitializer");
    }

    @Override
    public void enterArguments(PhpParser.ArgumentsContext ctx) {
//        print(ctx.getText(),"  ----enterArguments");
    }


    // 赋值语句顺序 enterAssignmentExpression->enterAssignable->enterChainExpression
    @Override
    public void enterAssignmentExpression(PhpParser.AssignmentExpressionContext ctx) {
//        println(ctx.getText(), "  ----enterAssignmentExpression");
    }

    // 赋值语句的左侧
    @Override
    public void enterAssignable(PhpParser.AssignableContext ctx) {
        String assignVarText = ctx.getText();
//        println(assignVarText,"  ----enterAssignable");
    }

    // $this->phpbb_root_path = $phpbb_root_path; 这种赋值语句不会进来，只会取 phpbb_root_path
    // $phpbb_root_path
    // $php_ext
    // $this->load_tasks($tasks); 表达式语句
    @Override
    public void enterChainExpression(PhpParser.ChainExpressionContext ctx) {
        // TODO 这里有点乱，情况比较复杂，需要重点写，考虑各种 上游情况
        //String chainText = ctx.getText();
        //String newChainText = rule.varReplaceDollar(chainText);
       // println(chainText,"  ----enterChainExpression");
    }

    @Override
    public void enterInnerStatement(PhpParser.InnerStatementContext ctx) {
        println(ctx.getText(), "----enterInnerStatement");
    }

    @Override
    public void enterStatement(PhpParser.StatementContext ctx) {
        String chainText = ctx.getText();
        String newChainText = rule.varReplaceDollar(chainText);
//        println(chainText,"  ----enterStatement");
    }

    @Override
    public void enterActualArgument(PhpParser.ActualArgumentContext ctx) {
        // 1.
        String arguText = ctx.getText();
        String newValName = rule.varReplaceDollar(arguText);
//        println(arguText, "  ----enterActualArgument");
    }

    @Override
    public void enterReturnStatement(PhpParser.ReturnStatementContext ctx) {
//        println(ctx.getText(),"  ----enterReturnStatement");
    }

    @Override
    public void enterForeachStatement(PhpParser.ForeachStatementContext ctx) {
//        println(ctx.getText(),"  ----enterForeachStatement");
    }

    @Override
    public void enterForStatement(PhpParser.ForStatementContext ctx) {
//        println(ctx.getText(),"  ----enterForStatement");
    }

    @Override
    public void enterIfStatement(PhpParser.IfStatementContext ctx) {
//        println(ctx.getText(),"  ----enterIfStatement");
    }

    @Override
    public void enterElseIfStatement(PhpParser.ElseIfStatementContext ctx) {
//        println(ctx.getText(),"  ----enterElseIfStatement");
    }

    @Override
    public void enterWhileStatement(PhpParser.WhileStatementContext ctx) {
//        println(ctx.getText(),"  ----enterWhileStatement");
    }

    @Override
    public void enterDoWhileStatement(PhpParser.DoWhileStatementContext ctx) {
//        println(ctx.getText(),"  ----enterDoWhileStatement");
    }

    @Override
    public void enterSwitchStatement(PhpParser.SwitchStatementContext ctx) {
//        println(ctx.getText(),"  ----enterSwitchStatement");
    }



    private void println(String s, String note) {
//        System.out.println(s + note);
//        builder.append(s + "\n");
    }

    private void print(String s, String note) {
//        System.out.print(s + note);
//        builder.append(s);
    }


    public StringBuilder getBuilder() {
        return builder;
    }
}
